package com.ruoyi.web.controller.qiniu;

import com.alibaba.fastjson2.JSONException;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.util.Objects;

@RestController
@RequestMapping("/oss/pic")
public class OssController extends BaseController {
    @Log(title = "文件上传")
    @PostMapping("/upload")
    public AjaxResult uploadImg(@RequestParam("file") MultipartFile file, HttpServletRequest request) throws JSONException {
        String contentType = file.getContentType();
        //System.out.print(contentType);
        String fileName = System.currentTimeMillis()+file.getOriginalFilename();
        File file1 = new File(fileName);
        file1.getAbsolutePath();
        int code = 0;
        String filePath = "D:/E";
        //JSONObject jo = new JSONObject();//实例化json数据
        //Map<String,String> jo = new HashMap<>();
        if (file.isEmpty()) {
            code = 1;
            fileName = "";
        }
        try {
            uploadFile(file.getBytes(), filePath, fileName);
            code = 0;
        } catch (Exception e) {
            // TODO: handle exception
        }
        return Objects.requireNonNull(AjaxResult.success().put("code", code)).put("filename","/imctemp-rainy/"+fileName);
    }

    private void uploadFile(byte[] bytes, String filePath, String fileName) {

    }
}
